package api

import (
	"github.com/gin-gonic/gin"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	"go-caipu/pkg/services/admin/dict"
)

func (hs *HTTPServer) GetSysDictTypePage(c *gin.Context) {
	cmd := dict.GetSysDictTypePageCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	//列表暂不分页
	cmd.PageSize = 1000
	result, _, err := hs.dictService.GetSysDictTypePage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithData(c, result)
}
func (hs *HTTPServer) GetSysDictType(c *gin.Context) {
	cmd := dict.GetSysDictTypeCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.dictService.Get(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)

}
func (hs *HTTPServer) CreateSysDictType(c *gin.Context) {
	cmd := dict.SysDictType{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.dictService.Insert(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdateSysDictType(c *gin.Context) {
	cmd := dict.SysDictType{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.dictService.Update(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) RemoveSysDictType(c *gin.Context) {
	cmd := dict.RemoveSysDictTypeCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.dictService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

/**以下内容为字典数据相关接口*/

func (hs *HTTPServer) GetSysDictDataPage(c *gin.Context) {
	cmd := dict.GetSysDictDataPageCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, num, err := hs.dictService.GetSysDictDataPage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: num})
}

// GetDictDataOptions 查询数据选项
func (hs *HTTPServer) GetDictDataOptions(c *gin.Context) {

	result, err := hs.dictService.GetDictDataOptions(c.Request.Context(), c.Param("code"))
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

func (hs *HTTPServer) GetSysDictData(c *gin.Context) {
	cmd := dict.GetSysDictDataCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.dictService.GetDictData(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}
func (hs *HTTPServer) CreateSysDictData(c *gin.Context) {
	cmd := dict.SysDictData{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.dictService.InsertDictData(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdateSysDictData(c *gin.Context) {
	cmd := dict.SysDictData{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.dictService.UpdateDictData(c.Request.Context(), cmd)

	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) RemoveSysDictData(c *gin.Context) {
	cmd := dict.RemoveSysDictDataCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.dictService.RemoveDictData(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
